﻿using Commons.Cache;
using Entity.Project;
using System.Collections.Generic;
using Commons;
using System;
using Senparc.WebSocket;
using System.Linq;
using Bussiness.Mangement.Project;
using System.ComponentModel.DataAnnotations;
using Entity;
using System.IO;
using System.Text;

namespace NetCoreAPI.SSWDSocket
{
    public class SSWDSocketHelp
    {
        /// <summary>
        /// 获取词条
        /// </summary>
        /// <returns></returns>
        private static Dictionary<string,string> GetWord(out string errMsg)
        {
            errMsg = string.Empty;
            var words = new List<string[]>();
            dynamic type = new Program().GetType();
            string basePath = Path.GetDirectoryName(type.Assembly.Location);
            string staticFilePath = Path.Combine(basePath, @"StaticFiles\words.txt");
            if (File.Exists(staticFilePath))
            {
                StreamReader sr = File.OpenText(staticFilePath);
                string nextLine;
                while ((nextLine = sr.ReadLine()) != null)
                {
                    var arr = Commonfs.Split(nextLine, '-');
                    if (arr.Length==2)
                    {
                        words.Add(arr);
                    }
                }
                sr.Close();

                //随机一个
                if (words.Count>0)
                {
                    int index= new Random().Next(0, words.Count);
                    var result= new Dictionary<string, string>();
                    result.Add(words[index][0], words[index][1]);
                    return result;
                }
                else
                {
                    errMsg = "NOTREADYTXT.";
                    return null;
                }
            }
            else
            {
                errMsg = "NOTFINDPATH.";
                return null;
            }
        }


        public static Room CreateRoom(ReceiveData receiveData, User user, string roomKey, string connectionId, out string errorMsg)
        {
            errorMsg = string.Empty;
            var room = new Room(Commonfs.RndCodeToUpper(6,"SSWD",""));
            roomKey = room.RoomKey;
            RoomUser roomUser = new RoomUser() { ConnectionId = connectionId, GameStatus = GameStatus.None, IsConnection = true, Pictrue = user.HeadImgUrl, UserId = user.Id, UserName = user.NickName };
            room.Users.Add(roomUser);
            room.Messages.Add(new RoomMessage() { Message = $"{roomUser.UserName} 进入房间。", MessageType = MessageType.RoomMsg, UserId = user.Id });
            var cache = CacheService.GetCacheManager();
            var isS = cache.Set(roomKey, room, TimeSpan.FromDays(1), TimeSpan.FromDays(1));
            if (!isS)
            {
                errorMsg = "创建房间失败。SETCACHEERR";
                return null;
            }
            return room;
        }

        public static Room Join(ReceiveData receiveData, User user, string roomKey, string connectionId, out string errorMsg)
        {
            errorMsg = string.Empty;
            roomKey = roomKey.ToUpper();//转化为大写
            Room room = null;
            //查询房间是否存在
            var cache = CacheService.GetCacheManager();
            room = cache.Get<Room>(roomKey);
            if (room == null || string.IsNullOrWhiteSpace(room.RoomKey))
            {
                errorMsg = "未查询到房间。";
                return room;
            }
            //判断是否存在房间中
            var findIndex = room.Users.FindIndex(x => x.UserId == user.Id);
            if (findIndex>=0)
            {
                RoomUser roomUser = room.Users[findIndex];
                if (!roomUser.IsConnection)
                {
                    roomUser.IsConnection = true;
                    roomUser.ConnectionId = connectionId;
                    room.Messages.Add(new RoomMessage() { Message = $"{roomUser.UserName} 重新连接。", MessageType = MessageType.RoomMsg, UserId = user.Id });
                }
                if (roomUser.IsQuit)
                {
                    roomUser.IsQuit = false;
                    roomUser.ConnectionId = connectionId;
                    room.Messages.Add(new RoomMessage() { Message = $"{roomUser.UserName} 进入房间。", MessageType = MessageType.RoomMsg, UserId = user.Id });
                }
                roomUser.Pictrue = user.HeadImgUrl;
                roomUser.UserName = user.NickName;
            }
            else
            {
                RoomUser roomUser = new RoomUser() { ConnectionId = connectionId, GameStatus = GameStatus.None, IsConnection = true, Pictrue = user.HeadImgUrl, UserId = user.Id, UserName = user.NickName };
                room.Users.Add(roomUser);
                room.Messages.Add(new RoomMessage() { Message = $"{roomUser.UserName} 进入房间。", MessageType = MessageType.RoomMsg, UserId = user.Id });
            }
            var isS = cache.Set(roomKey, room, TimeSpan.FromDays(1), TimeSpan.FromDays(1));
            if (!isS)
            {
                errorMsg = "加入房间失败。SETCACHEERR";
                return null;
            }
            return room;
        }

        public static Room Ready(ReceiveData receiveData,User user,string roomKey,out string errorMsg)
        {
            errorMsg = string.Empty;
            Room room = null;
            //查询房间是否存在
            var cache = CacheService.GetCacheManager();
            room = cache.Get<Room>(roomKey);
            if (room == null || string.IsNullOrWhiteSpace(room.RoomKey))
            {
                errorMsg = "未查询到房间。";
                return room;
            }
            //查询用户
            int userIndex= room.Users.FindIndex(x => x.UserId == user.Id);
            if (userIndex==-1)
            {
                errorMsg = "未查询到用户。";
                return room;
            }
            // 更改用户状态为已准备
            if (room.IsStarting)
            {
                errorMsg = "游戏已开始。";
                return room;
            }
            room.Users[userIndex].IsReady = true;
            //添加游戏的准备信息 当前房间有人准备且未开始游戏 则展示准备信息 前端控制。
            room.Messages.Add(new RoomMessage() { UserId = user.Id, MessageType = MessageType.GameMsg, Message = $"{user.NickName} 已准备。" });
            var isS = cache.Set(roomKey, room, TimeSpan.FromDays(1), TimeSpan.FromDays(1));
            if (!isS)
            {
                errorMsg = "准备失败。SETCACHEERR";
                return null;
            }
            return room;
        }

        public static Room Start(ReceiveData receiveData,User user,string roomKey,out string errorMsg)
        {
            errorMsg = string.Empty;
            Room room = null;
            //查询房间是否存在
            var cache = CacheService.GetCacheManager();
            room = cache.Get<Room>(roomKey);
            if (room == null || string.IsNullOrWhiteSpace(room.RoomKey))
            {
                errorMsg = "未查询到房间。";
                return room;
            }
            
            //检查房间准备人数是否大于两人
            if (room.ReadyPlayer.Count<2)
            {
                errorMsg = "当前准备人数不足。";
                return room;
            }
            //生成词条
            var word = GetWord(out errorMsg);
            if (word==null || word.Keys.Count==0 || word.Values.Count==0)
            {
                return null;
            }
            
            //添加对局 修改发言顺序 分配词条
            GameRecord gr = new GameRecord() { Word=new Dictionary<GameStatus, string>(), CurrentNumber=0, UserWord=new Dictionary<int, string>(), UserIdTalkList=new List<int>(),CurrentTurn=0,
                TurnRecord =new List<List<TurnRecord>>(), Vote=new List<List<Vote>>() };
            gr.Word.Add(GameStatus.Dinting, word.Keys.First());
            gr.Word.Add(GameStatus.Normality, word.Values.First());
            
            #region 根据人数选择卧底
            var wdlist = new List<int>();//卧底用户id
            if (room.ReadyPlayer.Count > 1)//一个卧底
            {
                int num = new Random().Next(room.ReadyPlayer.Count);
                wdlist.Add(room.ReadyPlayer[num].UserId);
            }
            if (room.ReadyPlayer.Count > 6)//两个卧底
            {
                int num = new Random().Next(room.ReadyPlayer.Count);
                while (wdlist.Exists(x => x == num))
                {
                    num = new Random().Next(room.ReadyPlayer.Count);
                }
                wdlist.Add(room.ReadyPlayer[num].UserId);
            }
            if (room.ReadyPlayer.Count > 9)//三个卧底
            {
                int num = new Random().Next(room.ReadyPlayer.Count);
                while (wdlist.Exists(x => x == num))
                {
                    num = new Random().Next(room.ReadyPlayer.Count);
                }
                wdlist.Add(room.ReadyPlayer[num].UserId);
            } 
            #endregion

            #region 调整发言顺序
            int fyf = new Random().Next(room.ReadyPlayer.Count);
            var la = new List<int>();
            for (int i = 0; i < room.ReadyPlayer.Count; i++)
            {
                if (i < fyf)
                {
                    la.Add(room.ReadyPlayer[i].UserId);
                }
                else
                {
                    gr.UserIdTalkList.Add(room.ReadyPlayer[i].UserId);
                }
            }
            gr.UserIdTalkList = gr.UserIdTalkList.Concat(la).ToList();
            #endregion;

            #region 分配用户词
            foreach (var userId in gr.UserIdTalkList)
            {
                if (wdlist.Exists(x=>x==userId))
                {
                    gr.UserWord.Add(userId, gr.Word[GameStatus.Dinting]);
                }
                else
                {
                    gr.UserWord.Add(userId, gr.Word[GameStatus.Normality]);
                }
            }
            #endregion

            room.GameRecord = gr;
            room.IsStarting = true;
            room.IsEnd = false;
            //修改玩家状态
            for (int i = 0; i < room.Users.Count; i++)
            {
                room.Users[i].IsOut = false;
                room.Users[i].IsReady = true;
                room.Users[i].IsStarting = true;
            }

            //添加开始游戏的提示
            room.Messages.Add(new RoomMessage() { UserId = user.Id, MessageType = MessageType.GameMsg, Message = $"游戏已开始。" });

            var isS = cache.Set(roomKey, room, TimeSpan.FromDays(1), TimeSpan.FromDays(1));
            if (!isS)
            {
                errorMsg = "开始失败。SETCACHEERR";
                return null;
            }
            return room;
        }


        public static Room Talk(ReceiveData receiveData, User user, string roomKey, out string errorMsg)
        {
            errorMsg = string.Empty;
            Room room = null;
            //查询房间是否存在
            var cache = CacheService.GetCacheManager();
            room = cache.Get<Room>(roomKey);
            if (room == null || string.IsNullOrWhiteSpace(room.RoomKey))
            {
                errorMsg = "未查询到房间。";
                return room;
            }
            //查询用户
            int userIndex = room.Users.FindIndex(x => x.UserId == user.Id);
            if (userIndex == -1)
            {
                errorMsg = "未查询到用户。";
                return room;
            }
            //记录发言
            var gr = room.GameRecord;
            if (gr.UserIdTalkList[gr.CurrentNumber]==user.Id)
            {
                if (gr.TurnRecord.Count == gr.CurrentTurn)
                {
                    gr.TurnRecord.Add(new List<TurnRecord>());
                }
                gr.TurnRecord[gr.CurrentTurn].Add(new TurnRecord() { message = receiveData.Value, UserId = user.Id });
                gr.CurrentNumber++;
                //如果发言发完之后 则将下一个改为序号改为0 然后又前端判断进入投票阶段
                if (gr.CurrentNumber == room.ReadyPlayer.Count)
                {
                    gr.CurrentNumber = 0;
                }
                room.GameRecord = gr;
            }
            room.Messages.Add(new RoomMessage() { UserId = user.Id, MessageType = MessageType.UserMsg, Message = receiveData.Value });
            var isS = cache.Set(roomKey, room, TimeSpan.FromDays(1), TimeSpan.FromDays(1));
            if (!isS)
            {
                errorMsg = "发言失败。SETCACHEERR";
                return null;
            }
            return room;
        }

        public static Room Msg(ReceiveData receiveData, User user, string roomKey, out string errorMsg)
        {
            errorMsg = string.Empty;
            Room room = null;
            //查询房间是否存在
            var cache = CacheService.GetCacheManager();
            room = cache.Get<Room>(roomKey);
            if (room == null || string.IsNullOrWhiteSpace(room.RoomKey))
            {
                errorMsg = "未查询到房间。";
                return room;
            }
            //查询用户
            int userIndex = room.Users.FindIndex(x => x.UserId == user.Id);
            if (userIndex == -1)
            {
                errorMsg = "未查询到用户。";
                return room;
            }
            
            room.Messages.Add(new RoomMessage() { UserId = user.Id, MessageType = MessageType.UserMsg, Message = receiveData.Value });
            var isS = cache.Set(roomKey, room, TimeSpan.FromDays(1), TimeSpan.FromDays(1));
            if (!isS)
            {
                errorMsg = "发送失败。SETCACHEERR";
                return null;
            }
            return room;
        }

        public static Room Vote(ReceiveData receiveData, User user, string roomKey, out string errorMsg)
        {
            errorMsg = string.Empty;
            Room room = null;
            //查询房间是否存在
            var cache = CacheService.GetCacheManager();
            room = cache.Get<Room>(roomKey);
            if (room == null || string.IsNullOrWhiteSpace(room.RoomKey))
            {
                errorMsg = "未查询到房间。";
                return room;
            }
            //查询用户
            int userIndex = room.Users.FindIndex(x => x.UserId == user.Id);
            if (userIndex == -1)
            {
                errorMsg = "未查询到用户。";
                return room;
            }

            int voteId = 0;
            if (!int.TryParse(receiveData.Value, out voteId))
            {
                errorMsg = "投票失败。ERR：NOT INT32";
                return room;
            }
            var voteUser = room.Users.Find(x => x.UserId == voteId);
            if ((voteUser == null || voteUser==default) && voteId!=0)
            {
                errorMsg = "投票失败。ERR：NOT Find VoteUser";
                return room;
            }
            

            //记录投票
            var voteMsg = string.Empty;//投票结果
            var gr = room.GameRecord;
            bool isEndTurn = false;
            if (room.IsStarting && ( gr.Vote==null || gr.Vote.Count==0 || ((gr.Vote.Count - 1) == gr.CurrentTurn && gr.Vote[gr.CurrentTurn].Count < room.StartPlayer.Count)))
            {
                if (gr.Vote.Count == gr.CurrentTurn)
                {
                    gr.Vote.Add(new List<Vote>());
                }
                //查看当前轮次是否已经投票过
                if (gr.Vote[gr.CurrentTurn].Exists(x => x.UserId == user.Id))
                {
                    errorMsg = "请不要重复投票。";
                    return room;
                }
                gr.Vote[gr.CurrentTurn].Add(new Vote() { UserId=user.Id, VoteId=voteId});
                gr.CurrentNumber++;
                //如果发言发完之后 则将下一个改为序号改为0 然后又前端判断进入投票阶段
                if (gr.CurrentNumber == room.ReadyPlayer.Count)
                {
                    //公布投票结果 最多票的出局 平票无事发生
                    var voteResult = new Dictionary<int, int>();
                    foreach (var item in gr.Vote[gr.CurrentTurn])
                    {
                        if (item.VoteId==0)
                        {
                            continue;
                        }
                        if (voteResult.ContainsKey(item.VoteId))
                        {
                            voteResult[item.VoteId]++;
                        }
                        else
                        {
                            voteResult.Add(item.VoteId, 1);
                        }
                    }
                    var outVote = new List<Dictionary<int, int>>();
                    foreach (var item in voteResult)
                    {
                        if (outVote.Count==0)
                        {
                            var v = new Dictionary<int, int>();
                            v.Add(item.Key, item.Value);
                            outVote.Add(v);
                            continue;
                        }
                        if (item.Value==outVote[0].Values.FirstOrDefault())
                        {
                            var v = new Dictionary<int, int>();
                            v.Add(item.Key, item.Value);
                            outVote.Add(v);
                            continue;
                        }
                        if (item.Value< outVote[0].Values.FirstOrDefault())
                        {
                            continue;
                        }
                        if (item.Value > outVote[0].Values.FirstOrDefault())
                        {
                            outVote.Clear();
                            var v = new Dictionary<int, int>();
                            v.Add(item.Key, item.Value);
                            outVote.Add(v);
                            continue;
                        }
                    }
                    //如果outvote大于1则多人平票 平安无事
                    if (outVote.Count>1)
                    {
                        List<string> names = new List<string>();
                        foreach (var item in outVote)
                        {
                            var ruser = room.Users.Find(x => x.UserId == item.Keys.First());
                            if (ruser==null)
                            {
                                errorMsg = "未查询到用户。ERR：FindVOTEUSER";
                                return room;
                            }
                            names.Add(ruser.UserName);
                        }
                        voteMsg = $"{string.Join('、',names)} 平票，票数为：{outVote.First().Values.First()}。";
                    }
                    if (outVote.Count<1)
                    {
                        voteMsg = "本轮无人投票，无人出局。";
                    }
                    //如果outvote等于1则这个人被票出局
                    if (outVote.Count==1)
                    {
                        var ruser = room.Users.Find(x => x.UserId == outVote[0].Keys.First());
                        voteMsg = $"{ruser.UserName} 被投票出局，票数为：{outVote.First().Values.First()}。";
                    }
                    gr.CurrentNumber = 0;
                    gr.CurrentTurn++;
                    isEndTurn = true;
                }
                room.GameRecord = gr;
            }
            else
            {
                errorMsg = "未到投票环节！";
                return room;
            }

            room.Messages.Add(new RoomMessage() { UserId = user.Id, MessageType = MessageType.VoteMsg, Message = voteId==0?$"{user.NickName} 弃票了。":$"{user.NickName} 给 {voteUser.UserName} 投了一票。" });
            if (!string.IsNullOrWhiteSpace(voteMsg))
            {
                room.Messages.Add(new RoomMessage() { UserId = user.Id, MessageType = MessageType.VoteMsg, Message = voteMsg });
            }
            if (isEndTurn)
            {
                IsGameOver(ref room, out errorMsg);
            }
            var isS = cache.Set(roomKey, room, TimeSpan.FromDays(1), TimeSpan.FromDays(1));
            if (!isS)
            {
                errorMsg = "投票失败。SETCACHEERR";
                return null;
            }
            return room;
        }


        //Quit
        public static Room Quit(ReceiveData receiveData, User user, string roomKey, out string errorMsg)
        {
            errorMsg = string.Empty;
            Room room = null;
            //查询房间是否存在
            var cache = CacheService.GetCacheManager();
            room = cache.Get<Room>(roomKey);
            if (room == null || string.IsNullOrWhiteSpace(room.RoomKey))
            {
                errorMsg = "未查询到房间。";
                return room;
            }
            //查询用户
            int userIndex = room.Users.FindIndex(x => x.UserId == user.Id);
            if (userIndex == -1)
            {
                errorMsg = "未查询到用户。";
                return room;
            }
            RoomUser rUser = room.Users[userIndex]; 
            //退出房间 判断是否在游戏中 在游戏中 此人默认被投票出局然后进行游戏结束判定
            if (rUser.IsStarting)
            {
                room.Users[userIndex].IsOut = true;
                IsGameOver(ref room, out errorMsg);
            }
            //如果不在游戏中则直接退出房间
            room.Users[userIndex].IsQuit = true;
            
            room.Messages.Add(new RoomMessage() { UserId = user.Id, MessageType = MessageType.RoomMsg, Message = $"{rUser.UserName} 退出房间。" }) ;
            var isS = cache.Set(roomKey, room, TimeSpan.FromDays(1), TimeSpan.FromDays(1));
            if (!isS)
            {
                errorMsg = "投票失败。SETCACHEERR";
                return null;
            }
            return room;
        }

        /// <summary>
        /// 判断是否游戏结束 结束则改变房间状态以及玩家状态 
        /// </summary>
        /// <param name="room"></param>
        /// <param name="errorMsg"></param>
        /// <returns></returns>
        private static void IsGameOver(ref Room room,out string errorMsg)
        {
            errorMsg = string.Empty;
            //判断当前在玩玩家人数
            string wdc = room.GameRecord.Word[GameStatus.Dinting];
            List<int> wdId = new List<int>();
            foreach (var item in room.GameRecord.UserWord)
            {
                if (item.Value==wdc)
                {
                    wdId.Add(item.Key);
                }
            }
            //当卧底人数与平民人数一样是 卧底获胜
            if ((double)room.StartPlayer.Count/2.00<=(double)wdId.Count)
            {
                room.GameRecord.EndGame = GameStatus.Dinting;
                room.IsEnd = true;
                room.IsStarting = false;
                room.Messages.Add(new RoomMessage() { UserId = 0, MessageType = MessageType.GameMsg, Message = $"游戏结束，卧底获胜。" });

            }
            //当卧底全部出局时 平民获胜
            if (wdId.Count<=0)
            {
                room.GameRecord.EndGame = GameStatus.Normality;
                room.IsEnd = true;
                room.IsStarting = false;
                room.Messages.Add(new RoomMessage() { UserId = 0, MessageType = MessageType.GameMsg, Message = $"游戏结束，平民获胜。" });
            }
            if (room.IsEnd)
            {
                //修改玩家状态
                for (int i = 0; i < room.Users.Count; i++)
                {
                    room.Users[i].GameStatus = GameStatus.None;
                    room.Users[i].IsOut = false;
                    room.Users[i].IsReady = false;
                    room.Users[i].IsStarting = false;
                }
            }
        }

    }

    /// <summary>
    /// 接收的消息
    /// </summary>
    public class ReceiveData
    {
        /// <summary>
        /// 操作类型
        /// </summary>
        public SSWDOperation Operation { get; set; }
        /// <summary>
        /// 值
        /// </summary>
        public string Value { get; set; }
    }



    /// <summary>
    /// 操作类型
    /// </summary>
    public enum SSWDOperation
    {
        None,
        /// <summary>
        /// 创建房间
        /// </summary>
        Create,
        /// <summary>
        /// 加入房间
        /// </summary>
        Join,
        /// <summary>
        /// 准备
        /// </summary>
        Ready,
        /// <summary>
        /// 开始
        /// </summary>
        Start,
        /// <summary>
        /// 发言
        /// </summary>
        Talk,
        /// <summary>
        /// 消息
        /// </summary>
        Msg,
        /// <summary>
        /// 投票
        /// </summary>
        Vote,
        /// <summary>
        /// 弃票
        /// </summary>
        AbstainVote,
        /// <summary>
        /// 退出
        /// </summary>
        Quit
    }

    public class RoomUser
    {
        public string UserName { get; set; }
        public int UserId { get; set; }
        /// <summary>
        /// 头像
        /// </summary>
        public string Pictrue { get; set; }


        /// <summary>
        /// 是否准备就绪
        /// default：未准备 false
        /// </summary>
        public bool IsReady { get; set; }

        /// <summary>
        /// 是否出局
        /// </summary>
        public bool IsOut { get; set; }

        /// <summary>
        /// 是否退出房间
        /// </summary>
        public bool IsQuit { set; get; }
        /// <summary>
        /// socket连接id
        /// </summary>
        public string ConnectionId { get; set; }
        /// <summary>
        /// 是否连接
        /// </summary>
        public bool IsConnection { get; set; }
        /// <summary>
        /// 是否正在游戏
        /// </summary>
        public bool IsStarting { get; set; }

        /// <summary>
        /// 游戏身份 
        /// 卧底 正常
        /// </summary>
        public GameStatus GameStatus {get;set;}

        public RoomUser()
        {
            IsReady = false;
            IsOut = false;
            IsStarting = false;
            IsQuit = false;
        }

    }

    /// <summary>
    /// 游戏身份类型
    /// 卧底 正常
    /// </summary>
    public enum GameStatus
    {
        None,
        /// <summary>
        /// 卧底
        /// </summary>
        Dinting,
        /// <summary>
        /// 正常
        /// </summary>
        Normality
    }

    /// <summary>
    /// 房间类
    /// </summary>
    public class Room
    {
        [Key]
        public string RoomKey { get; set; }
        //用户集合
        public virtual List<RoomUser> Users { get; set; }
        /// <summary>
        /// 是否正在进行游戏
        /// </summary>
        public bool IsStarting { get; set; }

        public bool IsEnd { get; set; }
        /// <summary>
        /// 记录
        /// </summary>
        public List<RoomMessage> Messages { get; set; }

        /// <summary>
        /// 已准备玩家
        /// </summary>
        /// <returns></returns>
        public List<RoomUser> ReadyPlayer => Users.FindAll(x => x.IsReady).ToList();
        /// <summary>
        /// 未出局玩家
        /// </summary>
        /// <returns></returns>
        public List<RoomUser> StartPlayer => Users.FindAll(x => x.IsReady && !x.IsOut).ToList();

        public bool IsReadyAll => Users.Count == ReadyPlayer.Count;
       
        /// <summary>
        /// 游戏记录
        /// </summary>
        public GameRecord GameRecord { get; set; }

        public Room()
        {
            Users = new List<RoomUser>();
            IsStarting = false;
            IsEnd = true;
            Messages = new List<RoomMessage>();
        }
        public Room(string key)
        {
            IsStarting = false;
            IsEnd = true;
            RoomKey = key;
            Users = new List<RoomUser>();
            Messages = new List<RoomMessage>();
        }
    }

    /// <summary>
    /// 消息类型
    /// 房间/游戏/用户
    /// </summary>
    public enum MessageType
    {
        /// <summary>
        /// 房间信息
        /// </summary>
        RoomMsg,
        /// <summary>
        /// 游戏信息
        /// </summary>
        GameMsg,
        /// <summary>
        /// 用户消息
        /// </summary>
        UserMsg,
        /// <summary>
        /// 投票信息
        /// </summary>
        VoteMsg
    }

    /// <summary>
    /// 游戏记录
    /// </summary>
    public class GameRecord
    {
        /// <summary>
        /// 词
        /// </summary>
        public Dictionary<GameStatus, string> Word { set; get; }

        /// <summary>
        /// 用户词条
        /// id 词
        /// </summary>
        public Dictionary<int,string> UserWord { get; set; }

        /// <summary>
        /// 用户发言顺序
        /// </summary>
        public List<int> UserIdTalkList { get; set; }
        /// <summary>
        /// 轮次记录
        /// </summary>
        public List<List<TurnRecord>> TurnRecord { get; set; }

        /// <summary>
        /// 投票记录
        /// </summary>
        public List<List<Vote>> Vote { get; set; }

        /// <summary>
        /// 当前发言序号
        /// </summary>
        public int CurrentNumber { get; set; }

        /// <summary>
        /// 当前轮次
        /// </summary>
        public int CurrentTurn { get; set; }

        /// <summary>
        /// 谁胜利了
        /// </summary>
        public GameStatus EndGame { get; set; }
    }
    /// <summary>
    /// 轮次记录
    /// </summary>
    public class TurnRecord
    {
        public int UserId { get; set; }
        public string message { get; set; }
    }
    /// <summary>
    /// 投票记录
    /// </summary>
    public class Vote
    {
        /// <summary>
        /// 用户Id
        /// </summary>
        public int UserId { get; set; }
        /// <summary>
        /// 投票的用户id
        /// </summary>
        public int VoteId { get; set; }
    }

    public class RoomMessage
    {
        public DateTime dateTime { get; set; }
        public int UserId { get; set; }
        public MessageType MessageType { get; set; }
        public string Message { get; set; }
        public RoomMessage()
        {
            dateTime = DateTime.Now;
        }
    }
}
